package crawler

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"
	"time"

	"github.com/axgle/mahonia"
)

func hanci__init() {
	// url := "http://www.hydcd.com/cy/htm1/aa0038.htm" //fmt.Sprintf("http://www.hydcd.com/cy/htm%d/%s%s%s.htm", i, string(j), string(z), fmt.Sprintf("%04d", k))
	// go check(url)
	url := "http://www.hydcd.com/cy/htm1/aa0038.htm"

	queue := make(chan string, 3)

	go func() {
		queue <- url
	}()
	for uri := range queue {
		go check(uri, queue)
		time.Sleep(200 * time.Millisecond)
	}
}

type cyitem struct {
	Name     string
	Spell    string
	Explain  string
	Chuchu   string
	Sample   string
	Jianpin  string
	Jinyici  string
	Fanyici  string
	Xiehouyu string
	Dengmi   string
	Yongfa   string
	English  string
	Story    string
}

func check(url string, queue chan string) {
	// visited[url] = true
	//println("url size:", len(visited))
	client := &http.Client{}
	req, _ := http.NewRequest("GET", url, nil)
	// 自定义Header
	req.Header.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)")
	req.Header.Add("Accept-Charset", "gb2312")
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("http get error", err)
		return
	}
	//函数结束后关闭相关链接
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("read error", err)
		return
	}

	bodystr := mahonia.NewDecoder("gbk").ConvertString(string(body))
	// gnet.Warning(bodystr)

	nextUrl := findNextUrl(&bodystr) //rightstr[:index]
	nextUrl = urlJoin(nextUrl, url)

	item := &cyitem{}
	item.Name = findName((&bodystr))
	item.Spell = findWithKey(&bodystr, "<p>【拼音】： ")
	item.Chuchu = findWithKey(&bodystr, "<p>【出处】： ")
	item.Explain = findWithKey(&bodystr, "<p>【解释】： ")
	item.Sample = findWithKey(&bodystr, "<p>【举例造句】： ")
	item.Jianpin = findWithKey(&bodystr, "<p>【拼音代码】： ")
	item.Jinyici = findWithKey(&bodystr, "<p>【近义词】： ")
	item.Fanyici = findWithKey(&bodystr, "<p>【反义词】： ")
	item.Xiehouyu = findWithKey(&bodystr, "<p>【歇后语】： ")
	item.Dengmi = findWithKey(&bodystr, "<p>【灯谜】： ")
	item.Yongfa = findWithKey(&bodystr, "<p>【用法】： ")
	item.English = findWithKey(&bodystr, "<p>【英文】： ")
	item.Story = findWithKey(&bodystr, "<p>【故事】： ")
	bt, _ := json.Marshal(item)
	ioutil.WriteFile(fmt.Sprintf("html/%s", item.Name), bt, 0644)
	ioutil.WriteFile("latestUrl.txt", []byte(url), 0644)

}

func findNextUrl(body *string) string {
	begin := "后一篇：<a href=\""
	index := strings.Index(*body, begin)
	rightstr := (*body)[index+len(begin):]
	index = strings.Index(rightstr, "\"")
	return rightstr[:index]
}

func findName(body *string) string {
	begin := "<font color=\"#000000\"> 【成语】： "
	index := strings.Index(*body, begin)
	rightstr := (*body)[index+len(begin):]
	index = strings.Index(rightstr, " ")
	return rightstr[:index]
}

func findWithKey(body *string, key string) string {
	begin := key
	index := strings.Index(*body, begin)
	if index < 0 {
		return ""
	}
	rightstr := (*body)[index+len(begin):]
	index = strings.Index(rightstr, "\n")
	result := rightstr[:index]
	return GuolvHref(result)
}

func GuolvHref(content string) string {
	// println(content)
	index := strings.Index(content, "<a")
	if index < 0 {
		return content
	}

	leftstr := content[:index]
	// println(leftstr)
	rightstr := content[index:]
	// println(rightstr)
	index = strings.Index(rightstr, ">")
	rightstr = rightstr[index+1:]

	leftstr += rightstr

	leftstr = strings.Replace(leftstr, "</a>", "", 1)
	// println(leftstr)
	return GuolvHref(leftstr)
}
